'use strict'

var is = require('bpmn-js/lib/util/ModelUtil').is,
  isAny = require('bpmn-js/lib/features/modeling/util/ModelingUtil').isAny,
  getBusinessObject = require('bpmn-js/lib/util/ModelUtil').getBusinessObject,
  domQuery = require('min-dom').query,
  cmdHelper = require('../../../helper/CmdHelper'),
  elementHelper = require('../../../helper/ElementHelper'),
  eventDefinitionHelper = require('../../../helper/EventDefinitionHelper'),
  script = require('./implementation/Script')('language', 'body', true)

function getVariable () {
  // console.log('condition')
  // 获取输出变量
  var pathname = location.pathname
  var variableObj = JSON.parse(sessionStorage.getItem('DS_RESOURCE'))
  var variableArr = (variableObj[pathname] || {}).outputList || []
  var optionsStr = ''
  variableArr.forEach((ele, index) => {
    if (index === 0) {
      optionsStr += `<option value=${ele.value} selected>${ele.name}</option>`
    } else {
      optionsStr += `<option value=${ele.value}>${ele.name}</option>`
    }
  })
  return optionsStr
}
module.exports = function (group, element, bpmnFactory, translate) {
  var bo = getBusinessObject(element)

  if (!bo) {
    return
  }

  var conditionalEventDefinition = eventDefinitionHelper.getConditionalEventDefinition(element)
  if (!(is(element, 'bpmn:SequenceFlow') && isConditionalSource(element.source)) &&
    !conditionalEventDefinition) {
    return
  }
  group.entries.push({
    id: 'condition',
    label: translate('Condition'),
    // html: '<div class="bpp-row">' +
    //           '<label for="cam-condition-type">'+translate('Condition Type')+'</label>' +
    //           '<div class="bpp-field-wrapper">' +
    //             '<select id="cam-condition-type" name="conditionType" data-value>' +
    //               '<option value="expression">'+translate('Expression')+'</option>' +
    //               '<option value="script">'+translate('Script')+'</option>' +
    //               '<option value="" selected></option>' +
    //             '</select>' +
    //           '</div>' +
    //         '</div>' +

    //         // expression
    //         '<div class="bpp-row">' +
    //           '<label for="cam-condition" data-show="isExpression">'+translate('Expression')+'</label>' +
    //           '<div class="bpp-field-wrapper" data-show="isExpression">' +
    //             '<input id="cam-condition" type="text" name="condition" />' +
    //             '<button class="clear" data-action="clear" data-show="canClear">' +
    //               '<span>X</span>' +
    //             '</button>' +
    //           '</div>' +
    //           '<div data-show="isScript">' +
    //             script.template +
    //           '</div>' +
    //         '</div>',
    html: '<div class="bpp-row">' +
            '<label for="cam-condition-type">变量列表</label>' +
            '<div class="bpp-field-wrapper">' +
              '<select id="cam-condition-type" name="conditionType" data-value>' +
                // '<option value="expression">'+translate('Expression')+'</option>' +
                // '<option value="script">'+translate('Script')+'</option>' +
                // '<option value="" selected></option>' +
                getVariable() +
              '</select>' +
            '</div>' +
          '</div>' +

          // expression
          '<div class="bpp-row">' +
            '<label for="cam-condition">' + translate('Expression') + '</label>' +
            '<div class="bpp-field-wrapper">' +
              '<input id="cam-condition" type="text" name="condition" placeholder="输入形式如：>100、<=100" />' +
              '<button class="clear" data-action="clear" data-show="canClear">' +
                '<span>X</span>' +
              '</button>' +
            '</div>' +
          '</div>',
    get: function (element, propertyName) {
      var conditionalEventDefinition = eventDefinitionHelper.getConditionalEventDefinition(element)

      var conditionExpression = conditionalEventDefinition
        ? conditionalEventDefinition.condition
        : bo.conditionExpression
      var values = {}
      // conditionType = '';
      if (conditionExpression) {
        // var conditionLanguage = conditionExpression.language;
        // if (typeof conditionLanguage !== 'undefined') {
        //   // values = script.get(element, conditionExpression);
        //   values.condition = conditionExpression.get('body');
        // } else {
        //   values.conditionType = conditionExpression.$attrs.dataProps
        //   values.condition = conditionExpression.get('body');
        // }
        values.conditionType = conditionExpression.$attrs.dataProps
        values.condition = conditionExpression.get('body')
      }
      return values
    },

    set: function (element, values, containerElement) {
      var conditionType = values.conditionType
      var commands = []
      var conditionProps = {
        body: undefined,
        dataProps: undefined // 自定义属性
      }

      // if (conditionType === 'script') {
      //   // conditionProps = script.set(element, values, containerElement);
      // } else {
      //   var condition = values.condition;

      //   conditionProps.body = condition;
      // }
      var condition = values.condition

      conditionProps.body = condition
      conditionProps.dataProps = conditionType
      var conditionOrConditionExpression

      if (conditionType) {
        conditionOrConditionExpression = elementHelper.createElement(
          'bpmn:FormalExpression',
          conditionProps,
          conditionalEventDefinition || bo,
          bpmnFactory
        )

        var source = element.source

        // if default-flow, remove default-property from source
        if (source && source.businessObject.default === bo) {
          commands.push(cmdHelper.updateProperties(source, { 'default': undefined }))
        }
      }

      var update = conditionalEventDefinition
        ? { condition: conditionOrConditionExpression }
        : { conditionExpression: conditionOrConditionExpression }

      commands.push(cmdHelper.updateBusinessObject(element, conditionalEventDefinition || bo, update))

      return commands
    },

    validate: function (element, values) {
      var validationResult = {}

      // if (!values.condition && values.conditionType === 'expression') {
      //   validationResult.condition = 'Must provide a value';
      // }
      // else if (values.conditionType === 'script') {
      //   // validationResult = script.validate(element, values);
      // }
      if (!values.condition) {
        validationResult.condition = '表达式不能为空'
      }
      return validationResult
    },

    isExpression: function (element, inputNode) {
      var conditionType = domQuery('select[name=conditionType]', inputNode)
      if (conditionType.selectedIndex >= 0) {
        return conditionType.options[conditionType.selectedIndex].value === 'expression'
      }
    },

    isScript: function (element, inputNode) {
      var conditionType = domQuery('select[name=conditionType]', inputNode)
      if (conditionType.selectedIndex >= 0) {
        return conditionType.options[conditionType.selectedIndex].value === 'script'
      }
    },

    clear: function (element, inputNode) {
      // clear text input
      domQuery('input[name=condition]', inputNode).value = ''

      return true
    },

    canClear: function (element, inputNode) {
      var input = domQuery('input[name=condition]', inputNode)

      return input.value !== ''
    },

    script: script,

    cssClasses: [ 'bpp-textfield' ]
  })
}

// utilities //////////////////////////

var CONDITIONAL_SOURCES = [
  // 'bpmn:Activity', // 去掉除网关顺序流之外的条件选择
  'bpmn:ExclusiveGateway',
  // console.log('condition');
  'bpmn:ParallelGateway',
  'bpmn:InclusiveGateway',
  'bpmn:ComplexGateway'
]

function isConditionalSource (element) {
  return isAny(element, CONDITIONAL_SOURCES)
}
